﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatterns.StrategyPatterns
{
    class StrategyPattern
    {
        static void Main(string[] args)
        {
            //策略模式，分离策略和上下文，可自由切换策略，避免多策略的情况下，各种判断语句
            //但是策略本身的复用性太差，而且违背最小知识原则，必须弄懂每一个策略
            //在碰到不断改进，升级，扩展的算法时，使用策略模式非常适合

            //声明策略
            IStrategy strategy = new ConcreteStrategy1();
            //声明上下文
            Context context = new Context(strategy);
            //执行策略
            context.doAnything();
        }
    }
    /// <summary>
    /// 策略接口
    /// </summary>
    public interface IStrategy {
        void dosomething();
    }
    /// <summary>
    /// 策略1
    /// </summary>
    public class ConcreteStrategy1 : IStrategy
    {
        public void dosomething()
        {
            Console.WriteLine("Do Strategy1");
        }
    }
    /// <summary>
    /// 策略2
    /// </summary>
    public class ConcreteStrategy2 : IStrategy
    {
        public void dosomething()
        {
            Console.WriteLine("Do Strategy2");
        }
    }
    /// <summary>
    /// 封装角色
    /// </summary>
    public class Context {
        private IStrategy strategy = null;
        public Context(IStrategy _strategy) {
            this.strategy = _strategy;
        }
        public void doAnything()
        {
            this.strategy.dosomething();
        }
    }
}
